package com.wlcorp.forcazamis.web.action.baseinfo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.baseinfo.ICustomerService;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.domain.system.Department;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.WebUtil;
public class CustomerAction extends DispatchAction {
	
	public ActionForward customerManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService().getDepartmentList(null);
		request.setAttribute("departmentList",departmentList);
		return mapping.findForward("manager");
	}
	
	public ActionForward getCustomerList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Customer customer = new Customer();
		WebUtil.setRequestToObject(request,customer);
		ICustomerService service = ContextUtil.getInstance().getCustomerService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getCustomerCount(customer).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    customer.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
	    
		List<Customer> customerList = service.getCustomerList(customer, null);
		request.setAttribute("customerList",customerList);
		return mapping.findForward("list");
	}
	
	public ActionForward getCustomer(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Customer customer = new Customer();
		WebUtil.setRequestToObject(request,customer);
		customer = ContextUtil.getInstance().getCustomerService().getCustomerById(customer.getCustomerId());
		if(null == customer){
			customer = new Customer();
		}
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("staffList",staffList);
		request.setAttribute("customer",customer);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyCustomer(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String[] staffId=request.getParameterValues("staffId");
		
		Set<Staff> staffs=new HashSet<Staff>();
		for (int i = 0; i < staffId.length; i++) {
			Staff staff=new Staff();
			staff=ContextUtil.getInstance().getStaffService().getStaffById(Long.parseLong(staffId[i]));
//			staff.setStaffId(Long.parseLong(staffId[i]));
			staffs.add(staff);
		}
		Customer customer = new Customer();
		WebUtil.setRequestToObject(request,customer);
		customer.setStaffs(staffs);
		
		ContextUtil.getInstance().getCustomerService().saveCustomer(customer);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == customer.getStatus()){
			WebUtil.setAlertMessage(request,customer.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"操作成功！");
		}
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("staffList",staffList);
		request.setAttribute("customer",customer);
		return mapping.findForward("modify");
	}
	public ActionForward deleteCustomer(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Customer customer = new Customer();
		WebUtil.setRequestToObject(request,customer);
		customer = ContextUtil.getInstance().getCustomerService().getCustomerById(customer.getCustomerId());
		ContextUtil.getInstance().getCustomerService().deleteCustomer(customer);
		return getCustomerList(mapping,form,request,response);
	}
	
	public ActionForward selectCustomerTop(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		return mapping.findForward("selectListTop");
	}
	public ActionForward selectCustomerList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
//		Staff currentStaff = SessionValue.getInstance(request).getStaff();
//		Set<Customer> set=currentStaff.getCustomers();
		Customer customer = new Customer();
		WebUtil.setRequestToObject(request,customer);
		ICustomerService service = ContextUtil.getInstance().getCustomerService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getCustomerCount(customer).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    customer.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
	    Long staffId=SessionValue.getInstance(request).getStaff().getStaffId();
		List<Customer> customerList = service.getCustomerList(customer,staffId);
		
		request.setAttribute("customerList",customerList);
		return mapping.findForward("selectList");
	}
}
